<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>注册会员</title>
  <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport"/>
  <meta content="yes" name="apple-mobile-web-app-capable"/>
  <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
  <meta content="telephone=no" name="format-detection"/>
  <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <script src="__STATIC__/layer/layer.js"></script>
</head>
<body>
    <form action="javascript:;" method="post" >
        真实姓名：<input type="text" name="realname" value=""><br/>
        手机号：<input type="text" name="mobile" value=""><br/>
        验证码：<input type="text" name="msg_code" value=""><br/>
        <input type="button" value="获取验证码" onclick="get_msgcode()">
        密码：<input type="password" name="password" value=""><br/>
        密码：<input type="password" name="repassword" value=""><br/>
        <input type="button" value="提交" onclick="checkform();">
    </form>

</body>
</html>
<script>
    /**
     *  检查提交表单信息是否正确
     */
    function checkform(){
        var mb = /^1[34578]\d{9}$/;
        var ps = /^[a-zA-Z0-9_]{6,16}$/;
        var realname = $('input[name=realname]').val();
        var mobile = $('input[name=mobile]').val();
        var msg_code = $('input[name=msg_code]').val();
        var password = $('input[name=password]').val();
        var repassword = $('input[name=repassword]').val();
        if(realname == ''){
            alert('请输入真实姓名');
            return false;
        }

        if(mobile == ''){
            alert('请输入手机号');
            return false;
        }
        if(!mb.test(mobile)){
            alert('手机格式错误，请重新输入');
            return false;
        }
//        if(msg_code == ''){
//            alert('请输入短信验证码');
//            return false;
//        }
        if(password == ''){
            alert('请输入新密码');
            return false;
        }
        if(!ps.test(password)){
            alert('密码格式输入错误');
            return false;
        }
        if(password != repassword){
            alert('两次密码输入不一致，请重新输入');
            return false;
        }

        $.post("{:url('Login/register')}",
            {realname:realname,mobile:mobile,msg_code:msg_code,password:password,repassword:repassword},
            function(data){
                if(data.code == 1){
                    alert('注册成功');
                    window.location.href = "{:url('Index/index')}";
                    return false;
                }else{
                    alert(data.msg);
                    return false;
                }
            }
        );
    }

    /**
     * 获取短信验证码
     */
    function get_msgcode(){
        var reg = /^1[34578]\d{9}$/;
        var mobile = $('input[name=mobile]').val();
        if(mobile == ''){
            alert('请输入手机号');
            return false;
        }
        if(!reg.test($('input[name=mobile]').val())){
            alert('手机格式错误，请重新输入');
            return false;
        }
        $.post("{:url('Login/get_msg_code')}",
            {mobile:mobile},
            function(data){
                if(data.code == 1){
                    alert('发送成功');
                    return false;
                }else{
                    alert('发送失败');
                    return false;
                }
            }
        );
    }
</script>